﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microhard.Protal.Interfaces;
using Microhard.Core.DBUtility;
using Microhard.Protal.DataAccess;
using Microhard.Protal.DTO;
using Microhard.Core;
using Microhard.Protal.Resource;

namespace Microhard.Protal.Impl
{
    public class ProductImpl : IProduct
    {
        public Core.DBUtility.ExecutedResult Add(Models.ProductModel model)
        {
            ExecutedResult rtn = new ExecutedResult();
            MicrohardProtalEntities db = new MicrohardProtalEntities();
            Microhard_Product op = model.ToEntity();
            op.CreateTime = DateTime.Now;

            try
            {
                db.Microhard_Product.AddObject(op);
                db.SaveChanges();
            }
            catch (System.Exception ex)
            {
                rtn.ErrorMsg.Add("ProductImpl.Add", Msg.ProductImpl_Add);
                Log.Error("ProductImpl.Add", ex);
            }
            return rtn;
        }

        public Core.DBUtility.ExecutedResult Update(Models.ProductModel model)
        {
            ExecutedResult rtn = new ExecutedResult();
            MicrohardProtalEntities db = new MicrohardProtalEntities();
            Microhard_Product entity = db.Microhard_Product.SingleOrDefault(c => c.ProductID == model.ProductID);

            if (entity != null)
            {
                entity.Name = model.Name;
                entity.Decription = model.Decription;
                entity.CreateTime = DateTime.Now;
            }
            try
            {
                db.SaveChanges();
            }
            catch (Exception ex)
            {
                rtn.ErrorMsg.Add("ProductImpl.Update", Msg.CategoryImpl_Update);
                Log.Error("ProductImpl.Update", ex);
            }
            return rtn;
        }

        public Core.DBUtility.ExecutedResult Delete(List<Guid> ids)
        {
            throw new NotImplementedException();
        }

        public Core.DBUtility.ExecutedResult Delete(Models.ProductModel model)
        {
            throw new NotImplementedException();
        }

        public Models.ProductModel GetModelByID(Guid id)
        {
            if (id == Guid.Empty)
                return null;

            MicrohardProtalEntities db = new MicrohardProtalEntities();
            var rtn = db.Microhard_Product.SingleOrDefault(p => p.ProductID == id);

            if (rtn != null)
                return rtn.ToModel();

            return null;
        }

        public Models.ProductModel GetModelByModel(Models.ProductModel model)
        {
            throw new NotImplementedException();
        }

        public List<Models.ProductModel> Query(Models.ProductModel Model, int pageIndex, int pageSize, out int totalCount)
        {
            MicrohardProtalEntities db = new MicrohardProtalEntities();
            IEnumerable<Microhard_Product> entities = from b in db.Microhard_Product
                                                       where scope.PKID.Contains(b.ProductID)
                                                       select b;
            totalCount = entities.Count();
            var pagedEntities = entities.OrderByDescending(p => p.CreateTime).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            return pagedEntities.Select(m=>m.ToModel()).ToList();
        }

        public List<Models.ProductModel> Query(Models.ProductModel Model)
        {
            throw new NotImplementedException();
        }

        public Core.DBUtility.ScopeResult scope
        {
            set;
            get;
        }
    }
}
